package 仙岛求药;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static int N = 310, n, m;
    static char[][] g = new char[N][N];
    static int[][] st = new int[N][N];
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            n = in.nextInt();
            m = in.nextInt();
            if (n == 0 && m == 0) {
                break;
            }
            PII start = null;
            for (int i = 0; i < n; i++) {
                String s = in.next();
                for (int j = 0; j < m; j++) {
                    g[i][j] = s.charAt(j);
                    if (g[i][j] == '@') {
                        start = new PII(i, j);
                    }
                }
            }
            System.out.println(bfs(start));
        }
    }

    private static int bfs(PII start) {
        Queue<PII> q = new LinkedList<>();
        q.offer(start);
        for (int i = 0; i < n; i++) {
            Arrays.fill(st[i], -1);
        }
        st[start.x][start.y] = 0;
        while (!q.isEmpty()) {
            PII t = q.poll();
            for (int i = 0; i < 4; i++) {
                int a = t.x + dx[i];
                int b = t.y + dy[i];
                if (a < 0 || a >= n || b < 0 || b >= m) {
                    continue;
                }
                if (g[a][b] == '#') {
                    continue;
                }
                if (st[a][b] != -1) {
                    continue;
                }
                st[a][b] = st[t.x][t.y] + 1;
                if (g[a][b] == '*') {
                    return st[a][b];
                }
                q.offer(new PII(a, b));
            }
        }
        return -1;
    }
}

class PII {
    int x, y;

    public PII(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
